package com.hmall.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
//@RequiredArgsConstructor //不用@Autowired
public class ItemService extends ServiceImpl<ItemMapper, Item> implements IItemService {

    @Autowired
    private ItemMapper itemMapper;

    @Override
    public PageDTO listPage(Integer page, Integer size) {

        PageHelper.startPage(page, size);
        Page<Item> list = itemMapper.pagelist();
        PageDTO pageDTO = new PageDTO();
        pageDTO.setList(list.getResult());
        pageDTO.setTotal(list.getTotal());
        return pageDTO;

    }


    /*根据id查询商品*/
    @Override
    public Item getItemById(Long id) {
        return itemMapper.findById(id);
    }

    /*新增商品 */
    @Override
    public boolean addItem(Item item) {
        return save(item);
    }

    /*商品上下架*/
    @Override
    public void updateItemStatus(Long id, int status) {
        update().set("status", status).eq("id", id).update();
    }

    /*修改商品*/
    @Override
    public boolean updateItem(Item item) {
        return updateById(item);
    }

    /*删除商品*/
    @Override
    public boolean deleteItemById(Long id) {
        return removeById(id);
    }
}
